package cn.newgrand.netcall.advertisement;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.PluginManager;
import org.jivesoftware.spark.util.log.Log;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-7-29 下午05:22:17 
 * 
 * 读取广告类
 * 
 */

public class AdXML {

	private static List<Advertisement> adMainWinList = new ArrayList<Advertisement>();
	private static List<Advertisement> adChatTopList = new ArrayList<Advertisement>();;
	private static List<Advertisement> adChatBottomList = new ArrayList<Advertisement>();

	private List<Advertisement> adList;
	private Advertisement advertisement;

	private ClassLoader classLoader;
	private static AdXML singleton;
	private static final Object LOCK = new Object(); 
	
	 public static AdXML getInstance() {
	        // Synchronize on LOCK to ensure that we don't end up creating
	        // two singletons.
	        synchronized (LOCK) {
	            if (null == singleton) {
	            	AdXML controller = new AdXML();
	                singleton = controller;
	                return controller;
	            }
	        }
	        return singleton;
	    }
	 private AdXML(){
		 loadAds();
	 }

	 /**
	  * 
	  * 加载所有的广告
	  * 
	  */
	public void loadAds() {
		final URL url = getClass().getClassLoader().getResource(
				"ads/ads.xml");
		try {
			InputStreamReader reader = new InputStreamReader(url.openStream());
			loadAd(reader);
		} catch (IOException e) {
			Log.error("Could not load ads.xml file.");
		}
	}

	/**
	 * Loads an internal plugin.
	 * 
	 * @param reader
	 *            the inputstreamreader for an internal plugin.
	 */
	private void loadAd(InputStreamReader reader) {

		int adId;
		String title ;
		String url;
		String subtitle;
		String image;
		String type;
		String content;
		
		SAXReader saxReader = new SAXReader();
		saxReader.setEncoding("UTF-8");
		Document pluginXML = null;
		try {
			pluginXML = saxReader.read(reader);
			pluginXML.setXMLEncoding("UTF-8");
		} catch (DocumentException e) {
			Log.error(e);
		}
		List ads = pluginXML.selectNodes("/ads/ad");
		for (Object ad1 : ads) {
			
			Element ad = (Element) ad1;
			adId = Integer.parseInt(ad.selectSingleNode("adId").getText());
			title = ad.selectSingleNode("title").getText();
			url = ad.selectSingleNode("url").getText();
			subtitle = ad.selectSingleNode("subtitle").getText();
			image = ad.selectSingleNode("image").getText();
			type = ad.selectSingleNode("type").getText();
			content = ad.selectSingleNode("content").getText();
			
			advertisement = new Advertisement(adId, title, subtitle, content,
					type, url, image);
			
			switch (type.charAt(0)) {
			
			case '1':
				adMainWinList.add(advertisement);
				break;
			case '2':
				adChatTopList.add(advertisement);
				break;
			case '3':
				adChatBottomList.add(advertisement);
				break;
			}

		}

	}
	
	public static List<Advertisement> getAdMainWinList(){
		return adMainWinList;
	}
	public static List<Advertisement> getAdChatTopList(){
		return adChatTopList;
	}
	public static List<Advertisement> getAdChatBottomList(){
		return adChatBottomList;
	}
	
	public static void main(String[] args){
		
		AdXML adXML = new AdXML();
		adXML.loadAds();
		
		for (Advertisement ad : adXML.getAdMainWinList()) {
			System.out.println(ad.getAdId());
		}
		
	}

}
